package team.you.ticket.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.you.ticket.dao.CarMapper;
import team.you.ticket.dao.TicketLogMapper;
import team.you.ticket.dao.TicketMapper;
import team.you.ticket.dao.TrainNumMapper;
import team.you.ticket.pojo.*;
import team.you.ticket.service.TicketService;

import javax.annotation.PreDestroy;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class PayTicketServiceImpl implements TicketService
{
    @Autowired
    private TicketMapper ticketMapper;
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private TicketLogMapper ticketLogMapper;
    @Autowired
    private TrainNumMapper trainNumMapper;
    @Override
    public Result add(TrainNum trainNum, String type, String remarks, HttpServletRequest request)
    {
        Result result=new Result();
        User user= (User) request.getSession().getAttribute("user");
        Ticket ticket1=ticketMapper.selectByUserId(user.getId(),trainNum.getId());
        if (ticket1!=null)
        {
            result.setCode(500);
            result.setMsg("你以购票！请勿重复购买！");
            return result;
        }
        else
        {
            String uuid= UUID.randomUUID().toString();
            Car car=carMapper.selectByCarNum(trainNum.getCarId());
            Integer number=car.getNumber()-trainNum.getPopleNum()+1;
            Ticket ticket=new Ticket(uuid,user.getId(),car.getId(),trainNum.getId(),type,trainNum.getMoney(),trainNum.getStarTime(),"0",
                    1,number,remarks);
            try {
                Integer i= ticketMapper.insert(ticket);
                if(i==1)
                {
                    String uuid1=UUID.randomUUID().toString();
                    Date time=new Date();
                    TicketLog ticketLog=new TicketLog(uuid1,user.getId(),uuid,time,0,"购票");
                    Integer j=ticketLogMapper.insert(ticketLog);
                    if (j==1)
                    {
                        Integer k=trainNumMapper.updateByPopleNum(trainNum.getId(),trainNum.getPopleNum()-1);
                        if (k==1)
                        {
                            result.setMsg("购票成功");
                            return result;
                        }
                        else
                        {
                            result.setMsg("信息修改失败");
                            return result;
                        }
                    }
                    else
                    {
                        result.setMsg("记录失败");
                        return result;
                    }
                }
                else
                {
                    result.setCode(500);
                    result.setMsg("购票失败");
                    return result;
                }
            }catch (Exception e)
            {
                e.printStackTrace();
                result.setCode(500);
                result.setMsg("系统繁忙");
                return result;
            }
        }
    }

    @Override
    public Result get(Integer page, HttpServletRequest request) {
        Result result=new Result();
        Integer limit=4;
        if(page<=0)
        {
            page=1;
            page=(page-1)*limit;
        }
        else page=(page-1)*limit;
        User user=(User) request.getSession().getAttribute("user");
        List<Ticket> tickets=ticketMapper.selectFlow(user.getId(),page,limit);
        result.setCount(tickets.size());
        result.setData(tickets);
        return result;
    }

    @Override
    public Result getPages() {
        Result result=new Result();
        Integer i=ticketMapper.getPages();
        result.setCount((int) Math.ceil(i/4.0));
        return result;
    }

    @Override
    public Result del(String ticketId) {
        Result result=new Result();
        Ticket ticket=ticketMapper.selectByPrimaryKey(ticketId);
        if (ticket!=null)
        {
            if (ticket.getType().equals("1") || ticket.getType().equals("2"))
            {
                result.setCode(500);
                result.setMsg("你已退票或票以过期");
                return result;
            }
            else
            {
                try {
                    Integer i=ticketMapper.updateByType(ticketId);
                    if (i==1)
                    {
                        String uuid=UUID.randomUUID().toString();
                        TicketLog ticketLog=new TicketLog(uuid,ticket.getUserid(),ticket.getId(),new Date(),0,"退票");
                        Integer j=ticketLogMapper.insert(ticketLog);
                        if (j==1)
                        {
                            result.setMsg("退票成功");
                            return result;
                        }
                       else
                        {
                            result.setMsg("信息写入失败");
                            return result;
                        }
                    }
                    else
                    {
                        result.setCode(500);
                        result.setMsg("退票失败");
                        return result;
                    }
                }catch (Exception e)
                {
                    e.printStackTrace();
                    result.setCode(500);
                    result.setMsg("系统繁忙");
                    return result;
                }
            }
        }
        else
        {
            result.setCode(500);
            result.setMsg("车票不存在");
            return result;
        }
    }
}
